<?php

namespace app\admin\controller;

use think\Request;
use think\facade\View;
use app\admin\model\Nav as Navs; 
use app\admin\model\NavFields;
use app\admin\model\NavBtns;
use app\admin\model\TplHooks;
use app\admin\model\Page;
use app\admin\model\FieldsConfig;
use app\admin\controller\Base;
use app\admin\controller\Msg;
class Nav extends Base
{
    public function parseField($field,Request $request){
        
        if(isset($field['search_config']->search_type) && $field['search_config']->search_type!='default'){
            $field['search_name'] = $field['search_config']->search_type.'['.$field['name'].']';
        }else{
            $field['search_name'] = $field['name'];
        }
        $optional_data_arr = [];
        if(in_array($field['type'],['select','checkbox','radio','bool']) && $field['optional_data']){
            if(substr($field['optional_data'],0,1)!='/' && substr($field['optional_data'],0,4)!='http'){
                $ar = explode("\r\n",$field['optional_data']);
                foreach($ar as $v){
                    $aar = explode('|',$v);
                    if(count($aar)==1){
                        $optional_data_arr[$v] = $v;
                    }else{
                        $optional_data_arr[$aar[0]] = $aar[1];
                    }
                }
            }else{
                if(substr($field['optional_data'],0,1)=='/'){
                    $field['optional_data'] = 'http://'.$request->host().$field['optional_data'];
                }
                $arrs = file_get_contents($field['optional_data']);
                $arrs = json_decode($arrs,true);
                if($arrs['code']==0){
                    $optional_data_arr = $arrs['data'];
                }
            }
        }
        if($field['type']=='config'){
            $configs = FieldsConfig::where('fields_id',$field['id'])->select()->toArray();
            $field['config'] = [];
            foreach($configs as $r){
                $field['config'][] = $this->parseField($r,$request);
            }
        }
        $field['optional_data_arr'] = $optional_data_arr;
        return $field;
    }
    public function show(Request $request){

        $id = $request->param('id');
        $info = Navs::find($id)->toArray();
        $info['fields'] = [];
        $fields = NavFields::where('nav_id',$id)->order('order asc,id asc')->select()->toArray();
        $btns = NavBtns::where('nav_id',$id)->select();
        $hooks = TplHooks::where('nav_id',$id)->select();
        $tplhooks = [];
        foreach($hooks as $r){
            if(!isset($tplhooks[$r['name']])) $tplhooks[$r['name']] = [];
            $tplhooks[$r['name']][] = $r;
        }
        $info['hooks'] = $tplhooks;
        $btnar = [];
        foreach($btns as $r){
            $btnar[$r['pos']][] = $r;
        }
        $info['btns'] = $btnar;
        $fieldss = [];

        $info['form_content'] = [];
        foreach($fields as $field){
            if($field['type']=='content') $info['form_content'][] = $field['name'];
            $info['fields'][] = $this->parseField($field,$request);
        }

        
        View::assign('nav',$info);
        return View::fetch('tpl/list');
    }
}
